#include "board.h"

//定义控制块
static rt_thread_t MODBUS_thread = RT_NULL;
static rt_thread_t period_thread = RT_NULL;
static rt_thread_t Connect_thread = RT_NULL;

//函数声明
static void modbus_thread_entry( void* parameter );
static void period_thread_entry( void* parameter );
static void Connect_thread_entry( void* parameter );

_Main_t mainPar;
_Sys_t  Sys;



//处理慢速任务，每秒运行一次
static void period_thread_entry( void* parameter )
{

//        //启动定时器 72M/psc/arr
    while ( 1 )
    {
        mainPar.tickSec = rt_tick_get()/1000;//获取时钟
        if(mainPar.tickSec%1 == 0){rt_kprintf("Run Time:%dH-%dM-%dS.\r\n",((mainPar.tickSec/60)/60),
				((mainPar.tickSec/60)%60),mainPar.tickSec%60);	 }	
        		
			GpioSet(GPIO_MDU_LED,0);

//			GpioSet(RS485_EN,0);
			rt_thread_delay( 500 ); /* 延时500个tick */
				
			GpioSet(GPIO_MDU_LED,1);

//			GpioSet(RS485_EN,1);
			rt_thread_delay( 500 ); /* 延时500个tick */
   }
}
//处理与上位机进行通讯的任务
static void modbus_thread_entry( void* parameter )
{
	MYI2C_Init(&SENx,2000,0x38);    //2000:读取数据周期2S;   0x38:AHT20地址
    while ( 1 )
    {
		MODS_Poll();
		rt_thread_delay( 10 );
		 
		 MYI2C_Handle(&SENx);   //不使用死延时则定时10ms调用一次该函数
		 
		rt_kprintf("RH = %0.2f",SENx.RH);
		rt_kprintf("T = %0.2f\n",SENx.T);	
		 
		writeMdodbusReg(MReg_T,SENx.T*100);
		writeMdodbusReg(MReg_RH,SENx.RH*100);
    }
}
//处理快速任务，每秒10次
static void Connect_thread_entry( void* parameter )
{
	IWDG_Feed_Init(IWDG_Prescaler_256,1000);//约为6秒复位
	writeMdodbusReg(MReg_SOFTV,1);//写软件版本号
    while ( 1 )
    {
		IWDG_Feed();	
			
		//寄存器更新
			Data_update();
      rt_thread_delay( 50 );
    }
}
int main( void )
{

	MODBUS_thread =                        
        rt_thread_create( "MODBUS",         
                          modbus_thread_entry,  
                          RT_NULL,           
                          1024,                 
                          3,                   
                          20 );    
    period_thread =                        
        rt_thread_create( "period",             
                          period_thread_entry,  
                          RT_NULL,           
                          1024,                 
                          4,                   
                          20 );  	
    Connect_thread =                        
        rt_thread_create( "Connect",             
                          Connect_thread_entry,  
                          RT_NULL,           
                          1024,                 
                          5,                   
                          20 ); 
	
	//地址
	//0x08为耗材灯
	//0x09为状态灯
	//0x0a为信号检测板
	mainPar.deviceAdd = 0x0A;

    if ( MODBUS_thread != RT_NULL )
        rt_thread_startup( MODBUS_thread );
    if ( period_thread != RT_NULL )
        rt_thread_startup( period_thread );
    if(Connect_thread != RT_NULL)
         rt_thread_startup( Connect_thread );
	return -1;

}



